package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbGenzongbangjiaoMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.BjgzGenzongbangjiaoInfoMainService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbGengzongbangjiaoResultVo;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 帮教工作管理 - 帮教管理 - 帮教信息 - 党政及家庭教育
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class BjgzGenzongbangjiaoInfoMainServiceImpl extends ServiceImpl<AbGenzongbangjiaoMapper,AbGengzongbangjiaoResultVo> implements BjgzGenzongbangjiaoInfoMainService {

    @Resource
    private AbGenzongbangjiaoMapper abGenzongbangjiaoMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;

    /**
     * 帮教工作管理 - 帮教管理 - 帮教信息 - 党政及家庭教育保存
     * @param dataStr 业务数据
     * @return Result
     */
    @Override
    public Result saveBjDzInfo(String dataStr,UserDTO user) {
        Date date = new Date();
        //转换实体类
        AbGengzongbangjiaoResultVo arv = JsonUtil.toBean(dataStr,AbGengzongbangjiaoResultVo.class);
        //新增
        if(StringUtils.isBlank(arv.getId())) {
            arv.setId(IdGen.strid());
            arv.setAdduserid(user.getDepartid());
            arv.setCreateUser(user.getUsername());
            arv.setCreateTime(date);
        }
        arv.setUpdateUser(user.getUsername());
        arv.setUpdateTime(date);
        boolean tag = arv.insertOrUpdate();
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取帮教 - 党政 - 信息
     * @param pp 帮教对象标志位
     * @return Result
     */
    @Override
    public Result getBjDzBaseInfo(String pp) {
        Map<String,Object> map = abGenzongbangjiaoMapper.getBjDzBaseInfo(pp);
        List<Map<String,Object>> appendixBjDzList = uploadAppendixFileMapper.getUploadAppendixFile(pp,null,"15");
        if(null != map && !map.isEmpty()) {
            map.put("appendixBjDzList",appendixBjDzList);
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }
}